<template>
    <el-dialog :title="title" v-model="open" width="680px" append-to-body @close="close">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="实例名称" prop="exampleName">
                <el-input v-model="form.exampleName" placeholder="实例名称"></el-input>
            </el-form-item>
            <el-form-item label="实例模型">
                <el-select v-model="form.modelId" placeholder="请选择模型"
                           @change="handleModelChange">
                    <el-option
                            v-for="agreementModel in agreementModelList"
                            :key="agreementModel.modelId"
                            :label="agreementModel.modelName"
                            :value="agreementModel.modelId">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div align="center" slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {addAgreementExample, updateAgreementExample} from "/src/api/property/example";
    import {getAgreementModelList} from "/src/api/property/model";

    export default {
        name: "update",
        emits: ['dialogStatusChanged'],
        props: {
            title: String,
            open: Boolean,
            form: Object,
        },
        data() {
            return {
                agreementModelList: [],
                // 表单校验
                rules: {
                    exampleName: [
                        {required: true, message: "实例名称不能为空", trigger: "blur"}
                    ],
                    modelId: [
                        {required: true, message: "实例模型不能为空", trigger: "blur"}
                    ]
                }
            }
        },
        created() {
            this.init()
        },
        computed: {
            //监测modelId
            monitorModelId() {
                return this.form.modelId
            },
        },
        watch: {
            monitorModelId(newVal) {

            }
        },
        methods: {
            //初始化
            init() {
                this.getAgreementModelListByParam()
            },
            //查询模型列表
            getAgreementModelListByParam() {
                getAgreementModelList(this.queryParams).then((response) => {
                    this.agreementModelList = response.data;
                })
            },
            //提交
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        if (this.form.exampleId !== undefined) {
                            updateAgreementExample(this.form).then(response => {
                                this.msgSuccess("修改成功");
                                this.close();
                            });
                        } else {
                            addAgreementExample(this.form).then(response => {
                                this.msgSuccess("新增成功");
                                this.close();
                            });
                        }
                    }
                })
            },
            //取消
            cancel() {
                this.$emit('dialogStatusChanged', false)
            },
            //关闭
            close() {
                this.$emit('dialogStatusChanged', false)
            },
            handleModelChange(val) {
                if (this.form.exampleId) {
                    this.$confirm('更改协议会删除原有配置?', "警告", {
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        type: "warning"
                    }).then(this.form.modelId = val)
                } else {
                    this.form.modelId = val
                }
            }
        }
    }
</script>

<style scoped>

</style>
